package no.progark.pong;

import no.progark.helikopter.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;

public class Controller extends View {
	private int posY;
	private int posX;
	private Bitmap image;
	//public final int CONTROLLERWIDTH = 92;
	//public final int CONTROLLERHEIGHT = 22;

	public int CONTROLLERWIDTH = 0;//46; //for testing with emulator
	public int CONTROLLERHEIGHT = 0;//11; //for testing with emulator
	
//	public Controller(Context context) {
//		super(context);
//		image = BitmapFactory.decodeResource(getResources(), R.drawable.pong_controller);
//		CONTROLLERHEIGHT = image.getHeight();
//		CONTROLLERWIDTH = image.getWidth();
//		velocity = 3;
//	}
	
	private static Controller instance = null;
	
	protected Controller(Context context){
		super(context);
		image = BitmapFactory.decodeResource(getResources(), R.drawable.pong_controller);
		CONTROLLERHEIGHT = image.getHeight();
		CONTROLLERWIDTH = image.getWidth();
	}
	
	public static Controller getInstance(Context context){
		if(instance == null) instance = new Controller(context);
		return instance;
	}

	public int getPosX(){
		return posX;
	}
	
	public void setPosX(int i) {
		posX =i;
		
	}
	
	public int getPosY(){
		return posY;
	}
	
	public Bitmap getImage(){
		return image;
	}
	
	public void initPos(int xpos, int ypos) {
		posX = xpos;
		posY = ypos;
	}
	
	public void update(int newXPos){
		/* old
		posX = (newXPos-posX > 0)? posX+40:posX-40;  
		*/
		
		int fingerControllerDistance = newXPos - posX;
		if(fingerControllerDistance < 0){ //Left of controller
			posX-=(CONTROLLERWIDTH*3/4);
		}
		else if(fingerControllerDistance > CONTROLLERWIDTH){ //Right of controller
			posX+=(CONTROLLERWIDTH*3/4);
		}
		//else finger is inside frame of controller, therefore do nothing.
		
	}
	
} 
